package com.aaa.cs.controller;

import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import com.aaa.cs.entity.Role;
import com.aaa.cs.service.RoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2020-12-01 14:38:59
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("通过主键查询单条数据")
    public Role selectOne(Integer id) {
        return this.roleService.queryById(id);
    }

    /**
     * 查询所有分页加条件查询
     *
     * @param pageNo   第几页
     * @param pageSize 每页显示数量
     * @param role     实体
     * @return Result
     */
    @PostMapping("queryAll")
    @ApiOperation("查询所有分页加条件查询")
    public Result queryAll(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestBody Role role) {
        return this.roleService.queryAll(pageNo, pageSize, role);
    }

    /**
     * 根据ID删除
     *
     * @param roleId id
     * @return Result
     */
    @DeleteMapping("deleteById")
    @ApiOperation("根据ID删除")
    public Result deleteById(Integer roleId) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.roleService.deleteById(roleId));
    }

    /**
     * 更新
     *
     * @param @param role 实例对象
     * @return Result
     */
    @RequestMapping("update")
    @ApiOperation("更新")
    public Result update(@ApiParam("更新json数据") @RequestBody Role role) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.roleService.update(role));
    }

    /**
     * 添加
     *
     * @param @param role 实例对象
     * @return Result
     */
    @PostMapping("insert")
    @ApiOperation("添加")
    public Result insert(@ApiParam("添加json数据") @RequestBody Role role) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.roleService.insert(role));
    }

    /**
     * 根据角色查询该角色拥有的所有权限id
     * @param roleId
     * @return
     */
    @RequestMapping("listMenuByRoleId")
    public List<Integer> listMenuByRoleId(int roleId){
        return this.roleService.listMenuByRoleId(roleId);
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping("page")
    // @RequestMapping(value = "page",method = RequestMethod.GET)
    @ApiOperation("角色列表")
    public Result list(@ApiParam("分页及模糊查询参数") @RequestParam Map map){
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.roleService.list(map));
    }

    /**
     * 添加角色权限关联数据
     * @param roleId
     * @param menuIds
     * @return
     */
    @RequestMapping("addRoleAndMenu")
    public Result addRoleAndMenu(Integer roleId,String menuIds){
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.roleService.addRoleAndMenu(roleId,menuIds));
    }



}